package com.example.queryexpressdelivery_01.common;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.queryexpressdelivery_01.entity.AdminData;
import com.example.queryexpressdelivery_01.entity.UserData;

import java.util.ArrayList;
import java.util.List;

public class DeliveryDAL {
    private DeliverySQLiteOpenHelper deliverySQLiteOpenHelper;
    private SQLiteDatabase sqliteDatabase;
    public Context context;
    public DeliveryDAL(Context context){
        this.context = context;
    }
    public void openDatabase() {
        deliverySQLiteOpenHelper = new DeliverySQLiteOpenHelper(context);
        try {
            sqliteDatabase = deliverySQLiteOpenHelper.getWritableDatabase();
        }catch (Exception e){
            sqliteDatabase = deliverySQLiteOpenHelper.getReadableDatabase();
        }
    }
    public void closeDatabase() {       // 关闭数据库，关闭helper
        if(sqliteDatabase != null){
            sqliteDatabase.close();
            sqliteDatabase = null;
        }
        if(deliverySQLiteOpenHelper != null){
            deliverySQLiteOpenHelper.close();
            deliverySQLiteOpenHelper = null;
        }
    }

    public List<UserData> queryUserAll(){

        List<UserData> userList = new ArrayList<>();
        try {
            openDatabase();
            Cursor cursor = sqliteDatabase.query("user",null,null,null,null,null, null,null);
            int idColumnIndex = cursor.getColumnIndex("id");
            int accountColumnIndex = cursor.getColumnIndex("account");
            int phoneColumnIndex = cursor.getColumnIndex("phone");
            int passwordColumnIndex = cursor.getColumnIndex("password");
            int emailColumnIndex = cursor.getColumnIndex("email");
            int dataColumnIndex = cursor.getColumnIndex("data");
            int lastlogintimeColumnIndex = cursor.getColumnIndex("lastlogintime");
            int statusColumnIndex = cursor.getColumnIndex("status");
            if(cursor.moveToFirst()){
                do{
                    int id = cursor.getInt(idColumnIndex);
                    String account = cursor.getString(accountColumnIndex);
                    String phone = cursor.getString(phoneColumnIndex);
                    String password = cursor.getString(passwordColumnIndex);
                    String email = cursor.getString(emailColumnIndex);
                    String data = cursor.getString(dataColumnIndex);
                    String lastlogintime= cursor.getString(lastlogintimeColumnIndex);
                    String status=cursor.getString(statusColumnIndex);
                    userList.add(new UserData(id,account,phone, password, email, data,lastlogintime,status));
                }while (cursor.moveToNext());
            }
            cursor.close();
            closeDatabase();
        }catch (Exception e){
            e.printStackTrace();
        }
        return userList;
    }
    public List<AdminData> queryAdminAll(){

        List<AdminData> adminList = new ArrayList<>();
        try {
            openDatabase();
            Cursor cursor = sqliteDatabase.query("admin",null,null,null,null,null, null,null);
            int idColumnIndex = cursor.getColumnIndex("id");
            int accountColumnIndex = cursor.getColumnIndex("account");
            int phoneColumnIndex = cursor.getColumnIndex("phone");
            int passwordColumnIndex = cursor.getColumnIndex("password");
            int emailColumnIndex = cursor.getColumnIndex("email");
            if(cursor.moveToFirst()){
                do{
                    int id = cursor.getInt(idColumnIndex);
                    String account = cursor.getString(accountColumnIndex);
                    String phone = cursor.getString(phoneColumnIndex);
                    String password = cursor.getString(passwordColumnIndex);
                    String email = cursor.getString(emailColumnIndex);
                    adminList.add(new AdminData(id,account,phone, password, email));
                }while (cursor.moveToNext());
            }
            cursor.close();
            closeDatabase();
        }catch (Exception e){
            e.printStackTrace();
        }
        return adminList;
    }


    public long insertUser(UserData user){
        long result = -1;
        ContentValues values = new ContentValues();
        values.put("account",user.getAccount());
        values.put("phone",user.getPhone());
        values.put("password",user.getPassword());
        values.put("email",user.getEmail());
        values.put("data",user.getData());
        values.put("lastlogintime",user.getLastlogintime());
        values.put("status",user.getStatus());
        try {
            openDatabase();
            result = sqliteDatabase.insert("user",null,values);
            closeDatabase();
        }catch (Exception e){

        }
        return result;
    }

        public UserData queryById(int id) {
            UserData user = null;
            Log.i("TAG","获取到了一个Id："+id);
            try {
                openDatabase();
                Cursor cursor = sqliteDatabase.query("user", null, "id=?",new String[]{String.valueOf(id)},null,null,null,null);
                Log.i("TAG",cursor.toString());
                if (cursor.moveToFirst()) {
                    int accountColumnIndex = cursor.getColumnIndex("account");
                    int phoneColumnIndex = cursor.getColumnIndex("phone");
                    int emailsColumnIndex = cursor.getColumnIndex("email");
                    int passwordColumnIndex = cursor.getColumnIndex("password");
                    int dataColumnIndex = cursor.getColumnIndex("data");
                    int lastlogintimeColumnIndex = cursor.getColumnIndex("lastlogintime");
                    int statusColumnIndex = cursor.getColumnIndex("status");
                    String account = cursor.getString(accountColumnIndex);
                    String phone = cursor.getString(phoneColumnIndex);
                    String email = cursor.getString(emailsColumnIndex);
                    String password = cursor.getString(passwordColumnIndex);
                    String data = cursor.getString(dataColumnIndex);
                    String last = cursor.getString(lastlogintimeColumnIndex);
                    String status = cursor.getString(statusColumnIndex);
                    user = new UserData(id, account, phone, password, email, data, last, status);
                }
                cursor.close();
                closeDatabase();
            } catch (Exception e) {
                Log.i("TAG","数据查询出错");
            }
            return user;
        }
    public long updateUser(UserData user){
        long result;
        ContentValues values = new ContentValues();
        values.put("account",user.getAccount());
        values.put("phone",user.getPhone());
        values.put("password",user.getPassword());
        values.put("email",user.getEmail());
        values.put("data",user.getData());
        values.put("lastlogintime",user.getLastlogintime());
        values.put("status",user.getStatus());
        try {
            openDatabase();
            result = sqliteDatabase.update("user",values,"id=?",new String[]{String.valueOf(user.getId())});
            closeDatabase();
        }catch (Exception e){
            result = -1;
        }
        return result;
    }
}

